var express = require("express")
var server = express()
var expressStatic = require("express-static")
var mysql = require("mysql")
var port = 81
var url = require("url")
var sql = mysql.createConnection({
	host:"localhost",
	user:"root",
	password:"123456",
	database:"aaaa",
	timezone:"08:00"
})
sql.connect()

server.get("/getdata",function(request,response){
	sql.query("select * from staff",function(error,data){
		if(error){
			console.log("查询失败")
		}
		else{
			response.end(JSON.stringify(data))
			console.log("查询成功")
		}
	})
})

// 添加数据
server.get("/addData",function(request,response){
	var name = url.parse(request.url,true).query.name
	var sex = url.parse(request.url,true).query.sex
	var date = url.parse(request.url,true).query.date
	var department = url.parse(request.url,true).query.department
	var pay = url.parse(request.url,true).query.pay
	var mobile = url.parse(request.url,true).query.mobile
	sql.query(`insert into staff (name,sex,date,department,pay,mobile) values("${name}","${sex}","${date}","${department}","${pay}","${mobile}")`,function(error,data){
		if(error){
			console.log("错误")
			response.end("error")
		}
		else{
			sql.query(`select * from staff`,function(error,data){
				if(!error){
					response.end(JSON.stringify(data))
				}
				else{
					response.end("no")
				}
			})
		}
	})
})

// 删除数据
server.get("/deleteData",function(request,response){
	var id = url.parse(request.url,true).query.id
	sql.query(`delete from staff where id="${id}"`,function(error,data){
		if(error){
			console.log("删除失败")
			response.end("no")
		}
		else{
			console.log("删除成功")
			response.end(JSON.stringify(data))
		}
	})
})

// 查询数据
server.get("/queryData",function(request,response){
	var id = url.parse(request.url,true).query.id;
	sql.query(`select * from staff where id="${id}"`,function(error,data){
		if(error){
			console.log("查询失败")
			response.end("1")
		}
		else{
			response.end(JSON.stringify(data))
			console.log("查询成功")
		}
	})
})

// 编辑数据
server.get("/editData",function(request,response){
	var id = url.parse(request.url,true).query.id;
	var name = url.parse(request.url,true).query.name
	var sex = url.parse(request.url,true).query.sex
	var date = url.parse(request.url,true).query.date
	var department = url.parse(request.url,true).query.department
	var pay = url.parse(request.url,true).query.pay
	var mobile = url.parse(request.url,true).query.mobile
	
	sql.query(`update staff set name = "${name}",sex = "${sex}",date = "${date}",department = "${department}",pay = "${pay}",mobile = "${mobile}" where id = "${id}"`,function(error,data){
		if(error){
			console.log("编辑失败")
			response.end("1")
		}
		else{
			sql.query(`select * from staff`,function(error,data){
				if(!error){
					console.log("编辑成功")
					response.end(JSON.stringify(data))
				}
			})
		}
	})
})



// __________________________________________________________


server.get("/getnews",function(request,response){
	var id = url.parse(request.url,true).query.id

	sql.query(`select * from news where id="${id}"`,function(error,data){
		if(error){
			console.log("错误")
			response.end("error")
		}else{
			response.end(JSON.stringify(data[0]))
			console.log("成功")
		}
	})
})

server.get("/newdata",function(request,response){
	sql.query(`select title,id from news`,function(error,data){
		if(error){
			console.log("错误")
			response.end("error")
		}else{
			response.end(JSON.stringify(data))
			console.log("成功")
		}
	})
})


server.get("/querynews",function(request,response){
	sql.query("select * from news",function(error,data){
		if(error){
			console.log("查询失败")
		}
		else{
			response.end(JSON.stringify(data))
			console.log("查询成功")
		}
	})
})

server.get("/addnews",function(request,response){
	var title = url.parse(request.url,true).query.title
	var time = url.parse(request.url,true).query.time
	var body = url.parse(request.url,true).query.body
	sql.query(`insert into news (title,time,body) values("${title}","${time}","${body}")`,function(error,data){
		if(error){
			console.log(error)
			response.end("error")
		}
		else{
			sql.query(`select * from news`,function(error,data){
				if(!error){
					response.end(JSON.stringify(data))
				}
				else{
					response.end("no")
				}
			})
		}
	})
})



server.get("/deletenews",function(request,response){
	var id = url.parse(request.url,true).query.id
	sql.query(`delete from news where id="${id}"`,function(error,data){
		if(error){
			console.log("删除失败")
			response.end("no")
		}
		else{
			console.log("删除成功")
			response.end(JSON.stringify(data))
		}
	})
})

server.use(expressStatic(`${__dirname}/static`))
server.listen(port)		